#include "stdafx.h"
#include "Texture.h"

Texture::Texture()
: m_iTextures(0), m_iTBO(0)
{}

Texture::~Texture()
{
	//Delete Texture Buffer if it isn't used
	if (m_iCloneCouter == 0)
	{
		glDeleteTextures(m_iTextures, m_iTBO);
	}
	SAFE_DEL_ARRAY(this->m_iTBO);
}

GLuint Texture::LoadTexture2D(char* strFile)
{
	return LoadTexture2D(&strFile, 1)[0];
}

GLuint* Texture::LoadTexture2D(char** strFiles, __UINT32 iTextures)
{
	m_eTextureTarget = GL_TEXTURE_2D;
	//Set textures count
	m_iTextures = iTextures;

	//Init list textures id
	if (!m_iTBO)
		SAFE_DEL_ARRAY(m_iTBO);
	m_iTBO = new GLuint[m_iTextures];

	//Generate Texture Buffer
	glGenBuffers(m_iTextures, m_iTBO);

	//Binding texture data
	for (int i = 0; i < m_iTextures; i++)
	{
		//Texture info
		int iWidth, iHeight, iBPP;
		char* imageData = NULL;
		GLenum eTextureFormat;

		//Load texture data from TGA file
		imageData = LoadTGA(strFiles[i], &iWidth, &iHeight, &iBPP);
		switch (iBPP)
		{
		case 24: //Texture format RGB color, 24-bit
			eTextureFormat = GL_RGB;
			break;
		case 32: //Texture format RGBA color, 32-bit
			eTextureFormat = GL_RGBA;
			break;
		default:
			glDeleteTextures(i, m_iTBO);
			SAFE_DEL_ARRAY(m_iTBO);
			SAFE_DEL_ARRAY(imageData);
			return NULL;
		}

		glBindTexture(m_eTextureTarget, m_iTBO[i]);
		glTexImage2D(GL_TEXTURE_2D, 0, eTextureFormat, iWidth, iHeight, 0, eTextureFormat, GL_UNSIGNED_BYTE, imageData);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glBindTexture(m_eTextureTarget, 0);
		SAFE_DEL_ARRAY(imageData);
	}

	return m_iTBO;
}

GLuint Texture::LoadTextureCube(char** strFiles, __UINT32 iCubeFaces)
{
	m_eTextureTarget = GL_TEXTURE_CUBE_MAP;
	//Set textures count
	m_iTextures = 1;

	//Init list textures id
	if (!m_iTBO)
		SAFE_DEL_ARRAY(m_iTBO);
	m_iTBO = new GLuint[m_iTextures];

	//Generate Texture Buffer
	glGenBuffers(m_iTextures, m_iTBO);

	//Binding texture data
	for (int i = 0; i < iCubeFaces; i++)
	{
		//Texture info
		int iWidth, iHeight, iBPP;
		char* imageData = NULL;
		GLenum eTextureFormat;

		//Load texture data from TGA file
		imageData = LoadTGA(strFiles[i], &iWidth, &iHeight, &iBPP);
		switch (iBPP)
		{
		case 24: //Texture format RGB color, 24-bit
			eTextureFormat = GL_RGB;
			break;
		case 32: //Texture format RGBA color, 32-bit
			eTextureFormat = GL_RGBA;
			break;
		default:
			glDeleteTextures(i, m_iTBO);
			SAFE_DEL_ARRAY(m_iTBO);
			SAFE_DEL_ARRAY(imageData);
			return NULL;
		}

		glBindTexture(m_eTextureTarget, m_iTBO[i]);
		glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, eTextureFormat, iWidth, iHeight, 0, eTextureFormat, GL_UNSIGNED_BYTE, imageData);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glBindTexture(m_eTextureTarget, 0);
		SAFE_DEL_ARRAY(imageData);
	}

	return m_iTBO[0];
}

__VOID Texture::SetTexture(Texture* texture)
{
	texture->m_iCloneCouter++;
	this->m_eTextureTarget = texture->m_eTextureTarget;
	this->m_iTextures = texture->m_iTextures;
	this->m_iTBO = m_iTBO;
	this->m_iCloneCouter = texture->m_iCloneCouter;
}